﻿var rolelist = {
    init: function () {
        $('#tableid').treegrid({
            fit: true,
            nowrap: false,
            border: false,
            rownumbers: true,
            animate: true,
            toolbar: toolbar,
            //collapsible: true,
            idField: 'ROLES_ID',
            treeField: 'ROLES_NAME',
            columns: [[
					{ title: lang.group_name, field: 'ROLES_NAME', width: 210 },
					//{ title: '角色状态', field: 'DELETEMARK', width: 80, align: 'center' },
					{title: lang.sequence, field: 'SORTCODE', width: 80, align: 'center' },
            //{ title: '创建用户', field: 'CREATEUSERNAME', width: 100, align: 'center' },
            //{ title: '创建时间', field: 'CREATEDATE', width: 140, align: 'center' },
            //{ title: '修改用户', field: 'MODIFYUSERNAME', width: 100 },
                    {title: lang.operation_pemission, field: 'ROLEOPRA', width: 300, align: 'center' },
					{ title: lang.group_description, field: 'ROLES_REMARK', width: 300, align: 'center' }
				]]
        });
        loadRoleData();
    },
    search: function () { loadRoleData(); }
}


var toolbar = [{
    text: lang.add,
    iconCls: 'icon-add',
    handler: function () { openPage("roleedit", "", "", lang.add_new_group); }
}, '-', {
    text: lang.modify,
    iconCls: 'icon-edit',
    handler: function () {
        var obj = $('#tableid').treegrid('getSelected');
        if (obj) {
            openPage("roleedit", "roleid=" + obj.ROLES_ID, "", lang.modify_group);
        }
        else {
            $.alert(lang.select_menu);
        }
    }
}, '-', {
    text: lang.delete_,
    iconCls: 'icon-remove',
    handler: function () {
        var obj = $('#tableid').treegrid('getSelected');
        if (obj) {
            deleteuser(obj.ROLES_ID, obj.ROLES_NAME);
        }
        else {
            $.alert(lang.select_group);
        }
    }
}, '-', {
    text: lang.Distribution_operation_pemission,
    iconCls: 'icon-sign',
    handler: function () {
        var obj = $('#tableid').treegrid('getSelected');
        if (obj) {
            openPage('rolerights', 'op=edit&roleid=' + obj.ROLES_ID, '', '[' + obj.ROLES_NAME + ']' + lang.Distribution_operation_pemission);
        }
        else {
            $.alert(lang.select_group);
        }
    }
}, '-', {
    text: lang.Distribution_file_operation_pemission,
    iconCls: 'icon-sign',
    handler: function () {
        var obj = $('#tableid').treegrid('getSelected');
        if (obj) {
            openPage('roleopraright', 'op=edit&roleid=' + obj.ROLES_ID, '', '[' + obj.ROLES_NAME + ']' + lang.Distribution_file_operation_pemission);
        }
        else {
            $.alert(lang.select_group);
        }
    }
}, '-', {
    text: lang.Distribution_table_operation_pemission,
    iconCls: 'icon-sign',
    handler: function () {
        var obj = $('#tableid').treegrid('getSelected');
        if (obj) {
            openPage('rolestructright', 'op=edit&roleid=' + obj.ROLES_ID, '', '[' + obj.ROLES_NAME + ']' + lang.Distribution_table_operation_pemission);
        }
        else {
            $.alert(lang.select_group);
        }
    }
}];

//#region 加载角色数据
function loadRoleData() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "RolesList",
        success: function (data) {
            if (data && data.length > 0) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.ROLES_ID = data[i].ROLES_ID;
                        obj.ROLES_NAME = data[i].ROLES_NAME;
                        obj.SORTCODE = data[i].SORTCODE;
                        obj.CREATEUSERNAME = data[i].CREATEUSERNAME;
                        obj.CREATEDATE = data[i].CREATEDATE;
                        obj.MODIFYUSERNAME = data[i].MODIFYUSERNAME;
                        obj.MODIFYDATE = data[i].MODIFYDATE
                        obj.ROLE_RESTRICTION = data[i].ROLE_RESTRICTION;
                        obj.DELETEMARK = data[i].DELETEMARK;
                        getsubRole(data, data[i].ROLES_ID, obj);
                        _data.push(obj);
                    }
                }
                $('#tableid').treegrid('loadData', _data);
            }

        }
    });

    function getsubRole(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.ROLES_ID = _d[i].ROLES_ID;
                    obj.DELETEMARK = _d[i].DELETEMARK;
                    obj.ROLES_NAME = _d[i].ROLES_NAME;
                    obj.ROLE_RESTRICTION = _d[i].ROLE_RESTRICTION;
                    obj.CREATEUSERNAME = _d[i].CREATEUSERNAME;
                    obj.CREATEDATE = _d[i].CREATEDATE;
                    obj.MODIFYUSERNAME = _d[i].MODIFYUSERNAME;
                    obj.MODIFYDATE = _d[i].MODIFYDATE
                    obj.SORTCODE = _d[i].SORTCODE;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubRole(_d, _sdata[j].ROLES_ID, _sdata[j]);
                }
            }
        }
    }
}

//#endregion

//#region 删除
function deleteuser(roleid, rolename) {
    $.extend($.messager.defaults, {
        ok: lang.ok,
        cancel: lang.cancel
    });
    $.confirm(lang.Are_you_sure_want_to_delete_the_current_group + "[" + rolename + "]", lang.delete_group, function (b) {
        if (b) {
            $.ajaxHandler({
                url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
                urlType: "EditRole",
                model: function () { return $.toJSON({ 'action': 0, 'ROLES_ID': roleid }); },
                success: function (data) {
                    $.alert(lang.Successfully_deleted, null, function () { rolelist.search(); });
                }
            });
        }
    });
}
//#endregion